package com.ruoyi.college.controller;

import java.util.*;
import java.util.stream.Collectors;

import com.ruoyi.college.domain.Class;
import com.ruoyi.college.service.IClassService;
import com.ruoyi.common.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.college.domain.Student;
import com.ruoyi.college.service.IStudentService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 学生信息Controller
 * 
 * @author ruoyi
 * @date 2025-08-25
 */
@Controller
@RequestMapping("/college/student")
public class StudentController extends BaseController
{
    private String prefix = "college/student";

    @Autowired
    private IStudentService studentService;

    @Autowired
    private IClassService classService;

    @RequiresPermissions("college:student:view")
    @GetMapping()
    public String student(ModelMap mmap)
    {
        Class aClass = new Class();
        mmap.put("classes", classService.selectClassList(aClass));
        return prefix + "/student";
    }

    /**
     * 查询学生信息列表
     */
    @RequiresPermissions("college:student:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Student student)
    {
        startPage();
        List<Student> list = studentService.selectStudentList(student);
        return getDataTable(list);
    }

    /**
     * 导出学生信息列表
     */
    @RequiresPermissions("college:student:export")
    @Log(title = "学生信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Student student)
    {
        List<Student> list = studentService.selectStudentList(student);
        ExcelUtil<Student> util = new ExcelUtil<Student>(Student.class);
        return util.exportExcel(list, "学生信息数据");
    }

    /**
     * 新增学生信息
     */
    @RequiresPermissions("college:student:add")
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("classes", classService.selectClassList(new Class()));
        return prefix + "/add";
    }

    /**
     * 新增保存学生信息
     */
    @RequiresPermissions("college:student:add")
    @Log(title = "学生信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Student student)
    {
        return toAjax(studentService.insertStudent(student));
    }

    /**
     * 修改学生信息
     */
    @RequiresPermissions("college:student:edit")
    @GetMapping("/edit/{studentId}")
    public String edit(@PathVariable("studentId") Long studentId, ModelMap mmap)
    {
        mmap.put("classes", classService.selectClassList(new Class()));
        Student student = studentService.selectStudentByStudentId(studentId);
        mmap.put("student", student);
        return prefix + "/edit";
    }

    /**
     * 修改保存学生信息
     */
    @RequiresPermissions("college:student:edit")
    @Log(title = "学生信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Student student)
    {
        return toAjax(studentService.updateStudent(student));
    }

    /**
     * 删除学生信息
     */
    @RequiresPermissions("college:student:remove")
    @Log(title = "学生信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(studentService.deleteStudentByStudentIds(ids));
    }

    @RequiresPermissions("college:student:import")
    @Log(title = "学生信息", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<Student> util = new ExcelUtil<Student>(Student.class);
        List<Student> studentList = util.importExcel(file.getInputStream());
        String operName = ShiroUtils.getSysUser().getLoginName();
        String message = studentService.insertBatchStudent(studentList, updateSupport, operName);
        return AjaxResult.success(message);
    }
    /**
     * 导出教师模板
     */
    @RequiresPermissions("college:student:exportTemplate")
    @Log(title = "导出学生模板", businessType = BusinessType.EXPORT)
    @GetMapping("/exportTemplate")
    @ResponseBody
    public AjaxResult exportTemplate()
    {
        ExcelUtil<Student> util = new ExcelUtil<>(Student.class);
        // 创建一个空的教师列表用于生成模板
        List<Student> list = Collections.emptyList();
        // 获取所有专业名称用于下拉框选项
        List<Class> classes = classService.selectClassList(new Class());
        List<String> classNames = classes.stream().map(Class::getClassName).collect(Collectors.toList());
        return util.exportExcelWithDropdowns(list, "学生信息数据", createDropdownMap(classNames), "学生模板.xlsx");    }

    /**
     * 创建下拉框映射关系
     *
     * @param classNames 专业名称列表
     * @return 下拉框映射
     */
    private Map<String, List<String>> createDropdownMap(List<String> classNames) {
        Map<String, List<String>> dropdownMap = new HashMap<>();
        // 假设"专业名称"列需要添加下拉框
        dropdownMap.put("所属班级", classNames);
        // 可以继续添加其他字段的下拉框选项
        List<String> sexptions = Arrays.asList("男", "女", "未知");
        dropdownMap.put("性别", sexptions);
        List<String> otherOptions = Arrays.asList("休学", "退学", "毕业", "在读");
        dropdownMap.put("状态", otherOptions);
        return dropdownMap;
    }


}
